package com.wanda.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanda.model.JsonGeneralModel;
import com.wanda.model.JsonModelGeneral;
import com.wanda.model.OrderModel;
import com.wanda.model.Pay_TypeModel;
import com.wanda.model.ProjectModel;
import com.wanda.model.RoomModel;
import com.wanda.model.UnitModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.OrderService;
import com.wanda.service.Pay_TypeService;
import com.wanda.service.ProjectService;
import com.wanda.service.RoomService;
import com.wanda.service.UnitService;
import com.wanda.service.Wd_UserService;

/**   
*    
* 项目名称：wanda_java2
* 类名称：OrderController   
* 类描述：   订单Controller层
* 创建人：SunBGLARE
* 创建时间：2016年5月27日 下午8:57:41   
* 版本：1.0.1     
*/
@Controller
@RequestMapping("qt/Order")   
public class OrderController { 
 
	@Resource
	private OrderService orderService;
	
	@Resource
	private ProjectService projectService;
	
	@Resource
	private UnitService unitService;
	
	@Resource
	private RoomService roomService;
	
	@Resource
	private Pay_TypeService pay_TypeService;

	@Resource
	private Wd_UserService wd_UserService;
	//查询全部订单
	@RequestMapping(value="/selectAll.do")
	public @ResponseBody JsonGeneralModel selectAll(){
		System.out.println("查询所有订单");
		JsonGeneralModel<OrderModel> jsonModel = new JsonGeneralModel();
		List<OrderModel> orders =null;
		try {
			//String delete_state="0"; 
			orders = orderService.selectAll();
			jsonModel.setRows(orders);
		}catch (Exception e) {
			e.printStackTrace();
		}
			return jsonModel;
	    }
	
	//新增订单
	@RequestMapping(value="/addOrder.do")
    public @ResponseBody Map<String,Object> add(String customer_name,
    		String pay_type_id,String room_id,
    		float deposit,String name,String orderType,
    		float unit_price,float total,float pay_area,
    		String create_time,String customer_id,String user_id,
    		String project_id,String unit_id,String build_id,String build_name,
    	    String order_type,
    	    HttpSession session
    		){
		Wd_UserModel user = (Wd_UserModel)session.getAttribute("bean");
		System.out.println("=============开始添加订单数据"); 
		OrderModel order = new OrderModel();
		order.setCustomer_id(customer_id);
		order.setCustomer_name(customer_name);
		order.setPay_type_id(pay_type_id);
		order.setRoom_id(room_id);
		order.setDeposit(deposit);
		order.setName(user.getName()); //session中获取
		order.setOrder_type(orderType);
		order.setUnit_price(unit_price);
		order.setTotal(total);
		order.setPay_area(pay_area);
		order.setCreate_time(create_time);
		order.setUser_id(user.getUser_id());
		order.setCreate_time(new Timestamp(System.currentTimeMillis()).toString());
		order.setCustomer_id(customer_id);  //session中获取
		order.setProject_id(project_id);
		order.setBuild_id(build_id);
		order.setUnit_id(unit_id);
		order.setBuild_name(build_name);
		order.setOrder_type(order_type);
		orderService.addOrder(order);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	System.out.println("添加成功"+order.getCreate_time()); 
    	map.put("msg","成功");
    	return map;
    }
	
	
	//删除订单  修改删除状态
	@RequestMapping(value="/deleteOrder.do")
    public @ResponseBody Map<String,Object> delete(String order_id){
		System.out.println("=============开始删除订单数据"+order_id); 
		OrderModel order = new OrderModel();
		order.setOrder_id(order_id);
		
		orderService.deleteOrder(order);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	System.out.println("添加成功"); 
    	map.put("msg","成功");
    	return map;
    }
	
	//通过拼接sql实现多条件查询
	@RequestMapping(value="/selectBySql.do")
	public @ResponseBody JsonGeneralModel selectBySql(String SearchSql){
		 System.err.println("SearchSql:"+SearchSql);
		 JsonGeneralModel<OrderModel> jsonModel = new JsonGeneralModel();
			List<OrderModel> orders =null;
			try {
				//delete_state="0";
				orders=orderService.selectBySql(SearchSql);
				jsonModel.setRows(orders);
			}catch (Exception e) {
				e.printStackTrace();
			}
			return jsonModel;  
	}
	
	//通过拼接sql实现多条件查询
	@RequestMapping(value="/selectOrderAndRoomBySql.do")
	public @ResponseBody JsonGeneralModel selectOrderAndRoomBySql(String SearchSql){
		System.out.println("=============多条件查询");
		 System.err.println("SearchSql:"+SearchSql);
		 JsonGeneralModel<OrderModel> jsonModel = new JsonGeneralModel();
			List<OrderModel> orders =null;
			try {
				//delete_state="0";
				orders=orderService.selectOrderAndRoomBySql(SearchSql);
				jsonModel.setRows(orders);
			}catch (Exception e) {
				e.printStackTrace();
			}
			return jsonModel;  
	}
	/**
	 * 
	* @Title: selectBySql 
	* @Description:
	* @param @param SearchSql
	* @param @return    
	* @return JsonGeneralModel    
	* @throws
	 */
	@RequestMapping(value="/project/selectBySql.do",produces="text/html;charset=UTF-8")
	public @ResponseBody String selectLikeProjeck(String Search){
		
		 System.err.println("selectLikeProjeck SearchSql:"+Search);
			List<ProjectModel> orders =null;
			String word = null;
			try {
				//delete_state="0";
				orders=projectService.selectBySql(Search);
				 word = "<words>";
					for(ProjectModel c:orders){
					  word +="<word project_id=\""+c.getProject_id()+"\">"+c.getProject_name()+"</word>";
				   System.out.println(c.getProject_id());
				   
					}
					word =word+"</words>";
			
			}catch (Exception e) {
				e.printStackTrace();
			}
			return word;
	}
	
	@RequestMapping(value="/unit/SelectBySql.do")
	public @ResponseBody JsonModelGeneral<UnitModel> showBySql(String seacherSql){
		System.err.println("SearchSql:"+seacherSql);
		JsonModelGeneral <UnitModel>json = new JsonModelGeneral<UnitModel>();
		List<UnitModel> units = null;
		try{
			units=unitService.selectBySql(seacherSql);
			json.setRows(units);
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return json;	
	}
	//查询
	@RequestMapping(value="/Pay_TypeSql/selectBySql.do")
	public @ResponseBody JsonModelGeneral<Pay_TypeModel> selectByPay_Type(String seacherSql){
		
		System.err.println("到controller啦 searchSql"+seacherSql);
		
		JsonModelGeneral <Pay_TypeModel>json = new JsonModelGeneral<Pay_TypeModel>();
		
		List<Pay_TypeModel> rooms = null;
		
		try{
			
			rooms = orderService.selectByPay_TypeSql(seacherSql);
			
			json.setRows(rooms);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return json;
	}
	@RequestMapping(value="/room/selectBySql.do")
	public @ResponseBody JsonModelGeneral<RoomModel> selectByRoomSql(String seacherSql){
		
		System.err.println("到controller啦 searchSql"+seacherSql);
		
		String sql = seacherSql+" and state='5'";
		
		JsonModelGeneral <RoomModel>json = new JsonModelGeneral<RoomModel>();
		
		List<RoomModel> rooms = null;
		
		try{
			
			rooms = roomService.selectBySql(sql);
			
			json.setRows(rooms);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return json;
	}
     public List<Wd_UserModel> selectUserOrgSql(String seacherSql){
		System.err.println("到controller啦 searchSql"+seacherSql);
		List<Wd_UserModel> rooms = null;
		try{
			
			rooms = wd_UserService.selectBySql(seacherSql);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return rooms;
	}
}